package string;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang3.RandomStringUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

public class String1 {

    @Test
    private void t3() {
        String s1 = "10";
        String s2 = "10 ";
        System.out.println(s1);
        System.out.println(s1.trim());
        System.out.println(s2);
        System.out.println(s2.trim());
    }

    public static void main(String[] args) {
        String a = new String("ac");
        String b = new String("ac");
        System.out.println((a.equals(b)));
        System.out.println(a==b);

        //length属性和length方法
        String c = "c1";
        System.out.println(c.length());

        //数组有length属性
        int[] i = new int[3];
        System.out.println(i.length);

        System.out.println("splitUsage");
        splitUsage();

    }


    @Test
    public void TestContains() {
        String s = "Spring";
        System.out.println(s.contains("Spring"));
    }


    public static void splitUsage () {
        String s = "102_1";
        String s2 = "103";
        String[] str1 = s.split("_");
        String[] str2 = s2.split("_");
        System.out.println("the length");
        System.out.println(str1.length);
        System.out.println(str2.length);
        System.out.println();
        Arrays.stream(str1).forEach(System.out::println);
        System.out.println();
        Arrays.stream(str2).forEach(System.out::println);

    }

    @Test
    public void t1() {
        // 1_12
        System.out.println("1" + "_" + 1 + 2);
        // 1_3
        System.out.println("1" + "_" + (1 + 2));
    }


    @Test
    private void t2() {
        String s1 = "QQ";


    }

    @Test
    private void t4() {
        String s1 = "QQ";
        String s2 = "QQ,WeChat";
        var plat = Arrays.stream(s2.split(","))
            .map(String::toLowerCase)
            .collect(Collectors.toList());
        System.out.println(plat);
        System.currentTimeMillis();
    }

    @Test
    private void t5() {
        byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);
        for (byte aByte : bytes) {
            System.out.print(aByte + " ");
        }
        System.out.println(Arrays.toString(bytes));
    }

    @Test
    private void t6() {
        int length = 4096;
        // 生成一个字节数为4096的串
        String str = RandomStringUtils.randomAlphanumeric(length);
        byte[] bytes = str.getBytes();
        Assert.assertEquals(bytes.length, length);
        System.out.println("the length of str " + str.length());
        Assert.assertEquals(str.length(), length);
        System.out.println(str);
        System.out.println(Arrays.toString(bytes));
        System.out.println(bytes);
    }


    /**
     * intern会向字符串常量池添加字符串，但是不会向堆中添加对象
     */
    @Test
    public void t7() {
        String a = "a";
        String b = new String("a");
        System.out.println(a == b);
        System.out.println(a == b.intern());


        // new出来的对象会向常量池添加对象，但是b2指向的是堆中的对象，a2 指向的是常量池中的对象
        String b2 = new String("bcfgz");
        String a2 = "bcfgz";
        System.out.println(a2 == b2);
        System.out.println(a2 == b2.intern());
    }






}
